import Vue from 'vue';
import VueRouter from 'vue-router';
import {
	Message
} from 'element-ui'
Vue.use(VueRouter);
import Mobile from '@/view/Mobile.vue'
import MobileHome from '@/view/mobile/MobileHome.vue';
import MobileHeader from '@/components/mobile/default/ModeblieHeader.vue'
import MobileFooter from '@/components/mobile/default/MobileFoot.vue'
import MobileInfo from '@/view/mobile/MobileInfo.vue'
import MobileBooKList from '@/view/mobile/MobileBooKList.vue'
import MobileBookDteail from '@/view/mobile/MobileBookDetail.vue'
import MobileSearch from '@/view/mobile/MobileSearch.vue'
import MoblieCart from '@/view/mobile/MoblieCart.vue'
import MoblieOrder from '@/view/mobile/MobileOrder.vue'
import MobileAddr from '@/view/mobile/MobileAddr.vue'
import MobileOrdeitem from '@/view/mobile/MobileOrdeitem.vue'
import MobilePay from '@/view/mobile/MobilePay.vue'
import MobileLogin from '@/components/mobile/MobileLogin.vue'
import UpdateInfo from '@/view/mobile/UpdateInfo.vue'
import MobileRegister from '@/view/mobile/MobileRegister.vue'
import PcHome from '../view/PcHome.vue'
import Home from '@/view/pc/Home.vue';
import PcHeader from '@/components/pc/default/PCHeader.vue'
import PcFooter from '@/components/pc/default/PCFooter.vue'
import PcRegister from '@/view/pc/Register.vue'
import PcCart from '@/view/pc/PcCart.vue'
import PcOrder from '@/view/pc/Order.vue'
import PcPay from '@/view/pc/PcPay.vue'
import PcBookDetail from '@/view/pc/PcBookDetail.vue'
import PcInfo from '@/view/pc/PcInfo.vue'
import InfoN from '@/components/pc/default/InfoNavigation.vue'
import InfoHome from '@/view/pc/InfoHome.vue'
import PcUpdatePwd from '@/view/pc/UpdatePwd.vue'
import PcUpdatePic from '@/view/pc/UpdatePic.vue'
import PcUpdateInfo from '@/view/pc/UpdateInfo.vue'
import PcMyOrder from '@/view/pc/MyOrder.vue'
import PcSearch from '@/view/pc/PcSearch.vue'
import Admin from '@/view/admin.vue';
import AdminHome from '@/view/admin/adminHome.vue'
import AdminHeader from '@/components/damin/default/adminHeader.vue';
import AdminNavigation from '@/components/damin/default/adminNavigation.vue'
import AdminHotImg from '@/view/admin/hotImg.vue'
import AddHot from '@/view/admin/addHot.vue'
import AdminType from '@/view/admin/admintype.vue'
import AdminType2 from '@/view/admin/admintype2.vue'
import Goods from '@/view/admin/goods.vue'
import AddGoods from '@/view/admin/addGoods.vue'
import Order from '@/view/admin/order.vue'
import Login from '@/components/Login.vue'
const routes = [{
		path: '/',
		// redirect:{name:'Home'}
		redirect: '/login'

	},
	{
		path: '/login',
		name: 'Login',
		component: Login
	},
	{
		path: '/pc',
		name: 'PcHome',
		component: PcHome,
		children: [{
				path: 'home',
				name: 'Home',
				components: {
					default: Home,
					header: PcHeader,
					footer: PcFooter,
				}
			},
			{
				path: 'register',
				name: 'PcRegister',
				components: {
					default: PcRegister,
					header: PcHeader,
				}
			},
			{
				path: 'cart',
				name: 'PcCart',
				components: {
					default: PcCart,
					header: PcHeader,
				}
			},
			{
				path: 'order',
				name: 'PcOrder',
				components: {
					default: PcOrder,
					header: PcHeader,
				}
			},
			{
				path: 'pay',
				name: 'PcPay',
				components: {
					default: PcPay,
					header: PcHeader,
				}
			},
			{
				path: 'bookDetail/:bookid',
				name: 'PcBookDetail',
				components: {
					default: PcBookDetail,
					header: PcHeader,
				}
			},
			{
				path: 'myOrder',
				name: 'PcMyOrder',
				components: {
					default: PcMyOrder,
					header: PcHeader,
				}
			},
			{
				path: 'search',
				name: 'PcSearch',
				components: {
					default: PcSearch,
					header: PcHeader,
				}
			},
			{
				path: 'info',
				name: 'PcInfo',
				component: PcInfo,
				children: [{
						path: 'infomain',
						name: 'InfoHome',
						components: {
							default: InfoHome,
							navigation: InfoN,
							header: PcHeader,
						}
					},
					{
						path: 'updatePwd',
						name: 'PcUpdatePwd',
						components: {
							default: PcUpdatePwd,
							navigation: InfoN,
							header: PcHeader,
						}
					},
					{
						path: 'updatePic',
						name: 'PcUpdatePic',
						components: {
							default: PcUpdatePic,
							navigation: InfoN,
							header: PcHeader,
						}
					},
					{
						path: 'updateInfo',
						name: 'PcUpdateInfo',
						components: {
							default: PcUpdateInfo,
							navigation: InfoN,
							header: PcHeader,
						}
					}
				]
			},
		]
	},
	{
		path: '/mobile',
		name: 'Mobile',
		component: Mobile,
		children: [{
				path: 'home',
				name: 'MobileHome',
				// 下面所使用的是：命名视图的方式，为router-view的name属性匹配组将
				components: {
					default: MobileHome, //渲染到没有name属性的router-view上
					header: MobileHeader,
					footer: MobileFooter
				}
			},
			{
				path: 'info',
				name: 'MobileInfo',
				// 下面所使用的是：命名视图的方式，为router-view的name属性匹配组将
				components: {
					default: MobileInfo, //渲染到没有name属性的router-view上
					header: MobileHeader,
					footer: MobileFooter
				}
			},
			{
				path: 'booKList',
				name: 'MobileBooKList',
				// 下面所使用的是：命名视图的方式，为router-view的name属性匹配组将
				components: {
					default: MobileBooKList, //渲染到没有name属性的router-view上
					header: MobileHeader,
					footer: MobileFooter
				}
			},
			{
				path: 'bookDteail/:bookid',
				name: 'MobileBookDteail',
				components: {
					default: MobileBookDteail,
					footer: MobileFooter
				}
			},
			{
				path: 'search',
				name: 'MobileSearch',
				component: MobileSearch
			},
			{
				path: 'cart',
				name: 'MobileCart',
				component: MoblieCart
			},
			{
				path: 'order',
				name: 'MoblieOrder',
				component: MoblieOrder
			},
			{
				path: 'addr',
				name: 'MobileAddr',
				component: MobileAddr
			},
			{
				path: 'orderitem',
				name: 'MobileOrderitem',
				component: MobileOrdeitem
			},
			{
				path: 'pay',
				name: 'MobilePay',
				component: MobilePay
			},
			{
				path: 'login',
				name: 'MobileLogin',
				component: MobileLogin
			},
			{
				path: 'updateinfo',
				name: 'UpdateInfo',
				component: UpdateInfo
			},
			{
				path: 'mobileregister',
				name: 'MobileRegister',
				component: MobileRegister
			},
		]
	},
	{
		path: '/admin',
		name: 'Admin',
		component: Admin,
		children: [{
				path: 'home',
				name: 'AdminHome',
				components: {
					default: AdminHome,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'img',
				name: 'AdminHotImg',
				components: {
					default: AdminHotImg,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'addHot',
				name: 'AddHot',
				components: {
					default: AddHot,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'type',
				name: 'AdminType',
				components: {
					default: AdminType,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'type2',
				name: 'AdminType2',
				components: {
					default: AdminType2,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'goods',
				name: 'Goods',
				components: {
					default: Goods,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'addGoods',
				name: 'AddGoods',
				components: {
					default: AddGoods,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
			{
				path: 'order',
				name: 'Order',
				components: {
					default: Order,
					header: AdminHeader,
					navigation: AdminNavigation,
				}
			},
		]
	}
];

const router = new VueRouter({
	routes,
});
// 全局路由守卫：会拦截所有的路由跳转行为
// 常用于实现登录拦截
router.beforeEach((to, from, next) => {
	// console.log(to, from)
	// 只对访问购物车组件的路由跳转行为进行拦截
	console.log(to.fullPath);
	if (to.fullPath == '/pc/cart' || to.fullPath == '/pc/myOrder' ||
		to.fullPath == '/mobile/cart' || to.fullPath == '/mobile/orderitem') {
		let token = window.localStorage.getItem('token')
		if (token) {
			next()
		} else {
			Message.error('未登录！请登录');
			next({
				path:'/login'
			})
		}
	} else {
		next()
	}
})
export default router;